import 'package:flutter/material.dart';
import 'package:scoped_model/scoped_model.dart';
import 'package:test_flutter/model/LanguageModel.dart';
import 'package:test_flutter/model/scoped_model/main_model.dart';
import 'package:test_flutter/pages/SplashPage.dart';
import 'package:test_flutter/utils/common_utils/CustomLocalizations.dart';
import 'package:test_flutter/utils/common_utils/NoticeTest.dart';
import 'package:test_flutter/utils/common_utils/SpHelper.dart';
import 'package:test_flutter/utils/common_utils/SpUtil.dart';
import 'package:test_flutter/utils/common_utils/log_util.dart';
import 'package:test_flutter/constant/resources.dart';
import 'package:flutter_localizations/flutter_localizations.dart'; // 导入flutter的包

void main() => runApp(new MyApp());

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => new _MyAppState();
}

MainModel mainModel = new MainModel();

class _MyAppState extends State<MyApp> {
  Locale _locale;
  //SpecificLocalizationDelegate _localeOverrideDelegate;
  @override
  void initState() {
    super.initState();

    //setLocalizedSimpleValues(localizedSimpleValues);
    setLocalizedValues(localizedValues);
    _initAsync();
    _initListener();

  }



  void _initAsync() async {
    await SpUtil.getInstance();
    if (!mounted) return;
    _loadLocale();
  }

  void _initListener() {
    NoticeTest.getInstance().ctrl.stream.listen((_) {
      _loadLocale();
    });
  }

  void _loadLocale() {
    setState(() {
      LanguageModel model = SpHelper.getLanguageModel();
      if (model != null) {
        LogUtil.e('LanguageModel: ' + model.toString());
        _locale = new Locale(model.languageCode, model.countryCode);
      } else {
        _locale = null;
      }
    });
  }



  @override
  Widget build(BuildContext context) {
    return ScopedModel<MainModel>(
      model: mainModel,
      child: new MaterialApp(
        home:  MaterialApp(
          title: 'My Application',
          theme: new ThemeData(
            primarySwatch: Colors.grey,
            brightness: Brightness.dark,
          ),
          locale: _locale,
          localizationsDelegates: [
            GlobalMaterialLocalizations.delegate,
            GlobalWidgetsLocalizations.delegate,
            CustomLocalizations.delegate
          ],
          supportedLocales: CustomLocalizations.supportedLocales,


          home: new SplashPage(),
        ),
      ),
    );
  }
}
